﻿using Aliyun.Acs.Dysmsapi.Model.V20170525;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ZBJF.Warranty.ALiAPI.Common;
using ZBJF.Warranty.ALiAPI.Model;
using ZBJF.Warranty.Common.Common;

namespace ZBJF.Warranty.ALiAPI
{
    public class ALiMessageHelper
    {
        ///// <summary>
        ///// 使用方式
        ///// </summary>
        //private void test()
        //{
        //    ALiMessageHelper mess = new ALiMessageHelper();
        //    IALiTemplateParam<OrderCheckModel> template = new OrderCheckParam(new OrderCheckModel("尺度","小熊", "8分钟"));
        //    bool result = mess.SendMessage("18046714158", template);
        //}


        #region 发送短信 + bool SendMessage(string phones, IALiTemplateParam<ITemplateModel> template)
        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="phones">电话号码；多个以‘,’分割</param>
        /// <param name="template"></param>
        /// <returns></returns>
        public bool SendMessage(List<string> phones, IALiTemplateParam<ITemplateModel> template)
        {
            if (phones == null || phones.Count() < 1)
            {
                return false;
            }
            string temp = string.Empty;
            foreach (string item in phones)
            {
                temp += item + ",";
            }
            return SendMessage(temp, template);
        }
        #endregion


        #region 发送短信 + bool SendMessage(string phones, IALiTemplateParam<ITemplateModel> template)
        /// <summary>
        /// 发送短信
        /// </summary>
        /// <param name="phones">电话号码；多个以‘,’分割</param>
        /// <param name="template"></param>
        /// <returns></returns>
        public bool SendMessage(string phones, IALiTemplateParam<ITemplateModel> template)
        {
            string param = template.GetTemplateParams();
            try
            {
                return this.ALiSendMessage(phones, template.TemplateCode, param);
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(string.Format("短信发送异常:{0}；手机号：{1}；短信模板：{2}；参数：{3}", ex.ToString(), phones, template.TemplateCode, param), LogType.error);
                return false;
            }
        }
        #endregion

        private bool ALiSendMessage(string phones, string templateCode, string templateParams)
        {
            if (string.IsNullOrEmpty(phones) || string.IsNullOrEmpty(templateCode))
            {
                return false;
            }
            SendSmsResponse response = new MessageService().SendMessage(phones, templateCode, templateParams);
            return response.Message.ToUpper() == "OK";
        }
    }
}
